package com.wawei.seeds.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Created by parker on 15/1/1.
 */
public class PrefUtils {

    private static final String PREF_LOGINED = "pref_logined";
    private static final String PREF_USER = "pref_user";
    private static final String PREF_PWD = "pref_pwd";
    private static final String PREF_LAST_SYNC = "lastSyncID";
    private static final String PREF_LAST_SYNC_BEAUTICIAN = "lastSyncBeauticianId";
    private static final String PREF_USER_PROFILE_IMG = "userProfileImg";
    private static final String PREF_USER_BKG_IMG = "userBkgImg";
    private static final String PREF_USER_ID = "userId";
    private static final String REFERENCE = "reference";
    private static final String PREF_FIRST_SHOWING = "first_showing";
    private static final String PREF_SHOWING_COUNT = "showing_count";

    public static String getUserPassword(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String user = sp.getString(PREF_USER, "");
        return user + "|||" + sp.getString(PREF_PWD, "");
    }

    public static void markUserPassword(final Context context, final String user, final String password){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putString(PREF_USER, user).putString(PREF_PWD, password).commit();
    }

    public static String getPrefUser(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(PREF_USER,"");
    }

    public static String getPrefPassword(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(PREF_PWD,"");
    }

    public static String isLogined(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(PREF_LOGINED,"");
    }

    public static void markLogined(final Context context, final String authToken){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putString(PREF_LOGINED,authToken).commit();
    }

    public static long getLastSyncSharingId(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getLong(PREF_LAST_SYNC, -1);
    }

    public static void markLastSyncSharingId(final Context context, final long id){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putLong(PREF_LAST_SYNC, id).commit();
    }
    public static void registerOnSharedPreferenceChangeListener(final Context context,
                                                                SharedPreferences.OnSharedPreferenceChangeListener listener) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.registerOnSharedPreferenceChangeListener(listener);
    }

    public static void unregisterOnSharedPreferenceChangeListener(final Context context,
                                                                  SharedPreferences.OnSharedPreferenceChangeListener listener) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.unregisterOnSharedPreferenceChangeListener(listener);
    }

    public static int getLastSyncBeauticianId(final Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getInt(PREF_LAST_SYNC_BEAUTICIAN, -1);
    }

    public static void markLastSyncBeauticianId(final Context context, final int beauticianId){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putInt(PREF_LAST_SYNC_BEAUTICIAN, beauticianId).commit();
    }

    public static void markUserProfileImgUri(final Context context, final String profileImg) {
        if(null != profileImg && profileImg.length() > 0) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
            sp.edit().putString(PREF_USER_PROFILE_IMG, profileImg).commit();
        }
    }

    public static String getPrefUserProfileImg(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(PREF_USER_PROFILE_IMG, "");
    }

    public static void markUserBackgroundImgUri(final Context context, final String backgroundImg) {
        if(null != backgroundImg && backgroundImg.length() > 0) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
            sp.edit().putString(PREF_USER_BKG_IMG, backgroundImg).commit();
        }
    }

    public static String getPrefUserBkgImg(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString(PREF_USER_BKG_IMG, "");
    }

    public static void markUserId(final Context context, int userId) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putInt(PREF_USER_ID, userId).commit();
    }

    public static int getUserId(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getInt(PREF_USER_ID, -1);
    }

    public static void markReference(final Context context, long reference) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putLong(REFERENCE, reference).commit();
    }

    public static long getReference(final Context context){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getLong(REFERENCE, -1);
    }

    public static int getFirstShowingItem(final Context context, String prefix){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getInt(prefix + PREF_FIRST_SHOWING, -1);
    }

    public static void markFirstShowingItem(final Context context, String prefix, int firstShowing){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putInt(prefix + PREF_FIRST_SHOWING, firstShowing).commit();
    }

    public static void clear(final Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().clear().commit();
    }
}
